package com.chinda.modules.sys.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.chinda.common.annotation.SysLog;
import com.chinda.common.enums.MessageEnum;
import com.chinda.common.exception.RRException;
import com.chinda.common.utils.R;
import com.chinda.modules.sys.dto.SysConfigDTO;
import com.chinda.modules.sys.entity.SysConfigEntity;
import com.chinda.modules.sys.qo.SysConfigQO;
import com.chinda.modules.sys.service.SysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;


/**
 * 系统配置信息表
 *
 * @author chinda
 * @date 2021-06-20 07:18:54
 */
@AllArgsConstructor
@RestController
@RequestMapping("/sysconfigs")
@Api(tags = "系统配置信息表")
public class SysConfigController {

    private final SysConfigService sysConfigService;

    /**
     * 分页
     *
     * @param qo
     * @return
     */
    @SysLog("查询系统配置信息表分页")
    @PostMapping("/page")
    @ApiOperation(value = "查询分页信息")
    public R page(@ApiParam(value = "qo", name = "查询实例", required = true) @RequestBody SysConfigQO qo) {
        return R.ok().set(sysConfigService.page(qo));
    }

    /**
     * 获取详情
     *
     * @return
     */
    @SysLog("查询系统配置信息表详情")
    @GetMapping("/{id}")
    @ApiOperation(value = "获取详情" )
    public R info(@PathVariable("id")  Long id) {
        SysConfigEntity sysConfig = sysConfigService.getById(id);
        return R.ok().set(sysConfig);
    }

    /**
     * 保存
     *
     * @return
     */
    @SysLog("保存系统配置信息表")
    @PostMapping
    @ApiOperation(value = "保存信息")
    public R save(@ApiParam(value = "sysConfigDTO", name = "系统配置信息表DTO实例", required = true) @RequestBody @Valid SysConfigDTO sysConfigDTO) {
        SysConfigEntity sysConfig = new SysConfigEntity();
        BeanUtil.copyProperties(sysConfigDTO, sysConfig, CopyOptions.create().ignoreNullValue());
        sysConfigService.save(sysConfig);
        return R.ok().set(sysConfig);
    }

    /**
     * 修改全属性
     *
     * @param sysConfigDTO
     * @return
     */
    @SysLog("修改系统配置信息表全属性")
    @PutMapping("/{id}")
    @ApiOperation(value = "修改全属性")
    public R modify(@PathVariable("id") Long id,
                    @ApiParam(value = "sysConfigDTO", name = "系统配置信息表DTO实例", required = true) @RequestBody @Valid SysConfigDTO sysConfigDTO) {
        SysConfigEntity sysConfig = new SysConfigEntity();
        BeanUtil.copyProperties(sysConfigDTO, sysConfig, CopyOptions.create().ignoreNullValue());
        sysConfig.setId(id);
        if (!sysConfigService.updateById(sysConfig)) {
            throw new RRException(MessageEnum.SYS_CHECK_0012.getMsg());
        }
        return R.ok().set(sysConfig);
    }

    /**
     * 修改局部属性
     *
     * @param id
     * @param sysConfigDTO
     * @return
     */
    @SysLog("修改系统配置信息表局部属性")
    @PatchMapping("/{id}")
    @ApiOperation(value = "修改局部属性")
    public R patch(@PathVariable("id") Long id,
                   @ApiParam(value = "sysConfigDTO", name = "系统配置信息表DTO实例", required = true) @RequestBody @Valid SysConfigDTO sysConfigDTO) {
        SysConfigEntity dbSysConfig = sysConfigService.getById(id);
        if (dbSysConfig == null) {
            throw new RRException(MessageEnum.SYS_CHECK_0022.getMsg());
        }
        BeanUtil.copyProperties(sysConfigDTO, dbSysConfig, CopyOptions.create().ignoreNullValue());
        if (!sysConfigService.updateById(dbSysConfig)) {
            throw new RRException(MessageEnum.SYS_CHECK_0012.getMsg());
        }
        return R.ok().set(dbSysConfig);
    }

    /**
     * 删除数据
     *
     * @param id
     * @return
     */
    @SysLog("删除系统配置信息表")
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除数据")
    public R remove(@PathVariable("id") Long id) {
        sysConfigService.removeById(id);
        return R.ok();
    }

    /**
     * 批量删除数据
     *
     * @param ids
     * @return
     */
    @SysLog("批量删除系统配置信息表")
    @DeleteMapping("/batch")
    @ApiOperation(value = "批量删除数据")
    public R remove(@ApiParam(value = "ids", name = "id集合", required = true) @RequestBody Long [] ids) {
        sysConfigService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 获取详情
     *
     * @return
     */
    @SysLog("根据key获取系统配置信息详情")
    @GetMapping("/param/{key}")
    @ApiOperation(value = "根据key获取系统配置信息详情" )
    public R getByParamKey(@PathVariable("key")  String paramKey) {
        SysConfigEntity sysConfig = sysConfigService.getByParamKey(paramKey);
        return R.ok().set(sysConfig);
    }

}
